package com.atguigu.service.impl;

import com.atguigu.dao.BookDao;
import com.atguigu.dao.OrderDao;
import com.atguigu.dao.OrderItemDao;
import com.atguigu.dao.impl.BookDaoImpl;
import com.atguigu.dao.impl.OrderDaoImpl;
import com.atguigu.dao.impl.OrderItemDaoImpl;
import com.atguigu.pojo.*;
import com.atguigu.service.OrderService;
import com.atguigu.utils.JDBCUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.UUID;

/**
 * @Author luoyin
 * @Date 11:42 2022/7/23
 **/
public class OrderServiceImpl implements OrderService {
    @Override
    public String checkOut(Cart existCart, User existUser) throws Exception {
        //开启事物
        JDBCUtils.startTransaction();
        try {
            //-----------生成订单(userId,existCart)-----------
            //用户id
            Integer userId = existUser.getUserId();
            //生成order
            Order order = new Order();
            String orderSequence = UUID.randomUUID().toString().replace("-","");
            order.setOrderSequence(orderSequence);
            String createTime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
            order.setCreateTime(createTime);
            order.setTotalCount(existCart.getTotalCount());
            order.setTotalAmount(existCart.getTotalAmount());
            order.setOrderStatus(BookstoreConstant.ORDER_UNPAY);
            order.setUserId(userId);
            OrderDao orderDao = new OrderDaoImpl();
            orderDao.add(order);

            //-----------批量添加订单项(orderId,CartItem)-----------
            Integer orderId = order.getOrderId();
            ArrayList<CartItem> cartItems = new ArrayList<>(existCart.getCartItemMap().values());
            //方式1
        /*List<OrderItem> orderItemList = new ArrayList<>();
        for (CartItem cartItem : cartItems) {
            OrderItem orderItem = new OrderItem();
            orderItem.setBookName(cartItem.getBookName());
            orderItem.setPrice(cartItem.getPrice());
            orderItem.setImgPath(cartItem.getImgPath());
            orderItem.setItemCount(cartItem.getCount());
            orderItem.setItemAmount(cartItem.getAmount());
            orderItem.setOrderId(orderId);
            orderItemList.add(orderItem);
        }
        for (OrderItem orderItem : orderItemList) {
            OrderItemDao orderItemDao = new OrderItemDaoImpl();
            orderItemDao.add(orderItem);
        }*/

            //方式2
            Object[][] orderItemParamsArr = new Object[cartItems.size()][6];
            for (int i = 0; i < cartItems.size(); i++) {
                CartItem cartItem = cartItems.get(i);
                orderItemParamsArr[i][0] = cartItem.getBookName();
                orderItemParamsArr[i][1] = cartItem.getPrice();
                orderItemParamsArr[i][2] = cartItem.getImgPath();
                orderItemParamsArr[i][3] = cartItem.getCount();
                orderItemParamsArr[i][4] = cartItem.getAmount();
                orderItemParamsArr[i][5] = orderId;
            }
            OrderItemDao orderItemDao = new OrderItemDaoImpl();
            orderItemDao.addBatch(orderItemParamsArr);

            //-----------批量修改Book(sales,stock)-----------
            Object[][] bookParamArr = new Object[cartItems.size()][3];
            for (int i = 0; i < cartItems.size(); i++) {
                CartItem cartItem = cartItems.get(i);
                bookParamArr[i][0] = cartItem.getCount();
                bookParamArr[i][1] = cartItem.getCount();
                bookParamArr[i][2] = cartItem.getBookId();
            }
            BookDao bookDao = new BookDaoImpl();
            bookDao.updateBatch(bookParamArr);

            //提交事务
            JDBCUtils.commit();
            return orderSequence;
        } catch (Exception e) {
            JDBCUtils.rollback();
            e.printStackTrace();
        }
        return null;
    }
}
